package com.base.cn.platform.os.config;

import com.base.cn.platform.os.common.interceptor.keyword.KeyWordFilter;
import com.base.cn.platform.os.common.xss.XssCodeFilter;
import com.base.cn.platform.os.interceptor.AppInterceptor;
import com.base.cn.platform.os.service.manage.op.keyword.OpKeywordService;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.web.servlet.WebMvcRegistrations;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.method.support.HandlerMethodReturnValueHandler;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;
import org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor;

import java.util.ArrayList;
import java.util.List;

/**
 * 接口配置对象
 *
 * @author s.li
 * @create 2018-09-20-16:06
 */
@Configuration
public class ApiConfig implements WebMvcConfigurer, InitializingBean {

    @Autowired
    private OpKeywordService opKeywordService;
    @Autowired
    private RequestMappingHandlerAdapter adapter;

    //敏感词、非字过滤路径
    private static List<String> urlList = new ArrayList<>();
    {
        urlList.add("/api/user/updateUserInfo");//修改用户信息接口
        urlList.add("/api/op/saveUpdateFeedBack");//用户提交反馈接口
        urlList.add("/api/qa/saveQuestionReply");//保存问答评论内容接口
        urlList.add("/api/qa/saveQuestionAdd");//添加问答追问内容接口
        urlList.add("/api/qa/saveQuestion");//问题提问接口
        urlList.add("/api/comment/saveComment");//保存课程评论内容
        urlList.add("/api/exam/record/saveQuestionRecord");//每道作答后保存作答结果接口
        urlList.add("/api/exam/record/saveExamQuestionCorrect");//试题纠错接口
        urlList.add("/api/topic/saveClassesTopic");//发表话题接口
        urlList.add("/api/topic/reply/saveTopicReply");//提交话题评论接口
        urlList.add("/api/topic/saveReportTopic");//保存举报话题内容接口
    }


    @Override
    public void afterPropertiesSet() throws Exception {
        List<HandlerMethodReturnValueHandler> returnValueHandlers = adapter.getReturnValueHandlers();
        List<HandlerMethodReturnValueHandler> handlers = new ArrayList(returnValueHandlers);
        this.decorateHandlers(handlers);
        adapter.setReturnValueHandlers(handlers);
    }

    private void decorateHandlers(List<HandlerMethodReturnValueHandler> handlers) {
        for (HandlerMethodReturnValueHandler handler : handlers) {
            if (handler instanceof RequestResponseBodyMethodProcessor) {
                MyHandlerMethodReturnValueHandler decorator = new MyHandlerMethodReturnValueHandler();
                int index = handlers.indexOf(handler);
                handlers.set(index, decorator);
                break;
            }
        }
    }

    /**
     * 添加自定义的结果处理器
     * @return
     */
    /*@Override
    public RequestMappingHandlerAdapter getRequestMappingHandlerAdapter() {
        return new RequestMappingHandlerAdapter(){
            @Override
            public void afterPropertiesSet() {
                // 由父类方法加载所有的 HandlerMethodReturnValueHandler
                super.afterPropertiesSet();
                // 不可修改的List
                List<HandlerMethodReturnValueHandler> returnValueHandlers = super.getReturnValueHandlers();
                // returnValueHandlers副本
                List<HandlerMethodReturnValueHandler> newReturnValueHandlers = new ArrayList<>();
                // 将自定义的Handler添加的首位
                newReturnValueHandlers.add(new MyHandlerMethodReturnValueHandler());
                //添加原来的Handler
                newReturnValueHandlers.addAll(returnValueHandlers);
                // 设置新值
                super.setReturnValueHandlers(newReturnValueHandlers);
            }
        };
    }*/

    /**
     * 前台用户登录拦截器
     * @return
     */
    @Bean
    public AppInterceptor appInterceptor(){
        return new AppInterceptor();
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(appInterceptor()).addPathPatterns("/**").excludePathPatterns("/api/pay/paySuccessCallback/**","/api/app/appDownload","/api/app/customerIndex");
    }

    @Bean
    public FilterRegistrationBean xssCodeFilter(){
        FilterRegistrationBean xssCodeFilter  = new FilterRegistrationBean();
        xssCodeFilter.setFilter(new XssCodeFilter());
        xssCodeFilter.setName("xssCodeFilter");
        xssCodeFilter.setUrlPatterns(urlList);
        xssCodeFilter.setOrder(2);
        return xssCodeFilter;
    }

    /**
     * 敏感词过滤
     * @return FilterRegistrationBean
     */
    @Bean
    public FilterRegistrationBean keyWordFilter(){
        FilterRegistrationBean keyWordFilter = new FilterRegistrationBean();
        keyWordFilter.setFilter(new KeyWordFilter(opKeywordService));
        keyWordFilter.setUrlPatterns(urlList);
        keyWordFilter.setName("keyWordFilter");
        keyWordFilter.setOrder(1);
        return keyWordFilter;
    }
}
